/**
 * 
 */
package com.zito.manager;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.downlove.common.DateUtils;
import com.downlove.common.RequestContext;
import com.downlove.database.QueryHelper;
import com.downlove.database.SequenceManager;
import com.zito.ErrMsgException;
import com.zito.bean.Advice;
import com.zito.common.Paginator;

/**
 * @author YanJiaXing
 * 
 */
public class AdviceManager {
	private Paginator paginator;
	private final Logger logger = LoggerFactory.getLogger(AdviceManager.class);

	public String getPaginator() {
		return this.paginator.getCssPageBlock();
	}

	public Paginator getPaginatorBean() {
		return this.paginator;
	}

	/**
	 * 查询列表
	 * 
	 * @param pagesize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Advice> getAdviceList(int pagesize) throws Exception {
		RequestContext rc = RequestContext.get();
		Advice obj = new Advice();
		int total = obj.TotalCount();
		this.paginator = new Paginator(rc, total, pagesize);
		// 设置当前页数和总页数
		int totalpages = this.paginator.getTotalPages();
		int curpage = this.paginator.getCurrentPage();
		if (totalpages == 0) {
			curpage = 1;
			totalpages = 1;
		}
		String sql = "SELECT id FROM Advice ORDER BY `IsReport`,`QuesTime` DESC";
		List<Long> list = QueryHelper.queryList(sql, curpage, pagesize);
		return obj.LoadList(list);
	}

	/**
	 * 根据ID查询详细信息
	 * 
	 * @return
	 */
	public Advice getAdviceInfo() throws Exception {
		RequestContext rc = RequestContext.get();
		long id = rc.param("id", -1);
		Advice obj = new Advice();
		obj = obj.Get(id);
		return obj;
	}
	
	/**
	 * 用户提交投诉
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean submitAdvice() throws Exception {
		RequestContext rc = RequestContext.get();
		Advice obj = rc.form(Advice.class);
		boolean re = false;
		String error = obj.doValidator();
		if (!StringUtils.isBlank(error))
			throw new ErrMsgException(error);
		
		obj.setId(SequenceManager.nextID(Advice.class.getSimpleName()));
		obj.setQuestime(DateUtils.getDateTime());
		obj.setQuesip(rc.ip());
		obj.setIsreport(0);
		obj.setIsshow(1);
		re = obj.Save()>0L;
		return re;
	}

	/**
	 * 管理员回复保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean SaveAdviceEdit() throws Exception {
		RequestContext rc = RequestContext.get();
		Advice obj = rc.form(Advice.class);
		boolean re = false;
		if (obj.getId() == -1L) {
			throw new Exception("保存失败！");
		}
		String error = obj.doValidator();
		if (!StringUtils.isBlank(error))
			throw new ErrMsgException(error);
		obj.setAnsid(MasterSession.get().getUserID());
		obj.setAnsname(MasterSession.get().getUserName());
		obj.setIsshow(0);
		re = obj.Update() > 0;
		return re;
	}
	
	/**
	 * 删除投诉
	 * @return
	 * @throws Exception
	 */
	public boolean deleteAdvice() throws Exception {
		RequestContext rc = RequestContext.get();
		Advice obj = new Advice();
		Long id = rc.param("id", -1L);
		boolean re = false;
		obj = obj.Get(id);
		if (obj != null){
			re = obj.Delete();
		}
		return re;
	}
}
